package org.csu.code.api.controller;

import org.csu.code.api.common.CommonResponse;
import org.csu.code.api.entity.Category;
import org.csu.code.api.entity.Item;
import org.csu.code.api.entity.Product;
import org.csu.code.api.service.CatalogService;
import org.csu.code.api.vo.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/catalog/")
public class CatalogController {

    @Autowired
    private CatalogService catalogService;

//    int num = 2;

    @GetMapping("categories")
    @ResponseBody
    //获取农产品大类Category信息，如蔬菜、鲜花、水果
    public CommonResponse<List<Category>> getCategoryList() {
        return catalogService.getCategoryList();
    }

    @GetMapping("categories/{id}")
    @ResponseBody
    //获取指定某农产品大类Category信息
    public CommonResponse<Category> getCategory(@PathVariable("id") String categoryId) {
        return catalogService.getCategory(categoryId);
    }

    @GetMapping("categories/{id}/products")
    @ResponseBody
    //获取指定某农产品大类Category下的细分农产品Product列表，如蔬菜下的土豆、番茄
    public CommonResponse<List<Product>> getProductListByCategoryId(@PathVariable("id") String categoryId) {
        return catalogService.getProductListByCategoryId(categoryId);
    }

    @GetMapping("products/{id}")
    @ResponseBody
    //获取指定细分农产品Product信息
    public CommonResponse<Product> getProductById(@PathVariable("id") String productId) {
        return catalogService.getProductById(productId);
    }

    @GetMapping("products/{id}/items")
    @ResponseBody
    //获取指定细分农产品Product下的商品item列表（农产品列表），如不同商家的土豆
    //记得调用精选评论接口，还没调
    public CommonResponse<List<ItemVO>> getItemsByProductId(@PathVariable("id") String productId) {
        return catalogService.getItemsByProductId(productId);
    }

    @GetMapping("items/{id}")
    @ResponseBody
    //农产品item详细信息（农产品详情）
    public CommonResponse<ItemVO> getItemById(@PathVariable("id") String itemId) {
        return catalogService.getItemById(itemId);
    }

    @PostMapping("/insales")
    @ResponseBody
    //农产品item上架
    public CommonResponse<String> insertItem(@RequestBody Item item) {
        return catalogService.insertItem(item);
    }

    @GetMapping("/deletesales/{id}")
    @ResponseBody
    //农产品item下架
    public CommonResponse<String> deleteItem(@PathVariable("id") int itemId) {
        return catalogService.deleteItem(itemId);
    }
}

